set rmsg on
set more off

************************************************************************
*
*	NOTE:		This do-file uses as input the final regression sample
*				and estimates equity and debt issuance regressions.
*
*************************************************************************

capture program drop _all
clear
clear matrix


* Define paths

	global 	data	"..."
			
	
*************************************************************************
**** Begin: Equity issuance ****
*************************************************************************	

	use "$data/GMMSV_reg_sample.dta", clear



**** Define control variables
global c sd_indcf5_lagatl1 l_al1 cf5_lagatl1 mbresid5

g dummy1 = 1 if ch_cash_at > 0 & ch_cash_at != .


**** Gross equity issuance to assets
g depvar = equity_issue_at
*
reghdfe depvar rho sigma_P sigma_A $c if dummy1 == 1, a(fyear) vce(cluster sic3)

* This estimation bootstraps the standard errors
*areg depvar rho sigma_P sigma_A $c if dummy1 == 1, a(fyear) vce(boot, seed(10101) reps(100) cluster(sic3) id(uisic3) group(gvkey))
est store ols1
	* Econ significance: evaluation points
	quietly summ sd_indcf5_lagatl1 if e(sample), det
		scalar cfvol_sd = r(sd)
	quietly summ depvar if e(sample), det
		scalar depvar_me = r(mean)	
	quietly summ rho if e(sample), det
		scalar rho_sd = r(sd)	
	quietly summ sigma_P if e(sample), det
		scalar sigP_sd = r(sd)			
	quietly summ sigma_A if e(sample), det
		scalar sigA_sd = r(sd)	
	* Evaluate effects
	nlcom	(RMEffCashVol     : _b[sd_indcf5_lagatl1]*cfvol_sd/depvar_me) ///
			(RMEffrho         : _b[rho]*rho_sd/depvar_me) ///		
			(RMEffsigP        : _b[sigma_P]*sigP_sd/depvar_me) ///
			(RMEffsigA        : _b[sigma_A]*sigA_sd/depvar_me) , post
est store econ_ols1
*
reghdfe depvar rho sigma_P sigma_A $c if dummy1 == 1, a(fyear sic2) vce(cluster sic3)

* This estimation bootstraps the standard errors
*areg depvar rho sigma_P sigma_A $c i.fyear if dummy1 == 1, a(sic2) vce(boot, seed(10101) reps(100) cluster(sic3) id(uisic3) group(gvkey))
est store ife1
	* Econ significance: evaluation points
	quietly summ sd_indcf5_lagatl1 if e(sample), det
		scalar cfvol_sd = r(sd)
	quietly summ depvar if e(sample), det
		scalar depvar_me = r(mean)	
	quietly summ rho if e(sample), det
		scalar rho_sd = r(sd)	
	quietly summ sigma_P if e(sample), det
		scalar sigP_sd = r(sd)			
	quietly summ sigma_A if e(sample), det
		scalar sigA_sd = r(sd)	
	* Evaluate effects
	nlcom	(RMEffCashVol     : _b[sd_indcf5_lagatl1]*cfvol_sd/depvar_me) ///
			(RMEffrho         : _b[rho]*rho_sd/depvar_me) ///		
			(RMEffsigP        : _b[sigma_P]*sigP_sd/depvar_me) ///
			(RMEffsigA        : _b[sigma_A]*sigA_sd/depvar_me) , post
est store econ_ife1

**** Gross equity issuance dummy
drop depvar
g depvar = gross_eq_issue_dummy
*
reghdfe depvar rho sigma_P sigma_A $c if dummy1 == 1, a(fyear) vce(cluster sic3)

* This estimation bootstraps the standard errors
*areg depvar rho sigma_P sigma_A $c if dummy1 == 1, a(fyear) vce(boot, seed(10101) reps(100) cluster(sic3) id(uisic3) group(gvkey))
est store ols2
	* Econ significance: evaluation points
	quietly summ sd_indcf5_lagatl1 if e(sample), det
		scalar cfvol_sd = r(sd)
	quietly summ depvar if e(sample), det
		scalar depvar_me = r(mean)	
	quietly summ rho if e(sample), det
		scalar rho_sd = r(sd)	
	quietly summ sigma_P if e(sample), det
		scalar sigP_sd = r(sd)			
	quietly summ sigma_A if e(sample), det
		scalar sigA_sd = r(sd)	
	* Evaluate effects
	nlcom	(RMEffCashVol     : _b[sd_indcf5_lagatl1]*cfvol_sd/depvar_me) ///
			(RMEffrho         : _b[rho]*rho_sd/depvar_me) ///		
			(RMEffsigP        : _b[sigma_P]*sigP_sd/depvar_me) ///
			(RMEffsigA        : _b[sigma_A]*sigA_sd/depvar_me) , post
est store econ_ols2
*
reghdfe depvar rho sigma_P sigma_A $c if dummy1 == 1, a(fyear sic2) vce(cluster sic3)

* This estimation bootstraps the standard errors
*areg depvar rho sigma_P sigma_A $c i.fyear if dummy1 == 1, a(sic2) vce(boot, seed(10101) reps(100) cluster(sic3) id(uisic3) group(gvkey))
est store ife2
	* Econ significance: evaluation points
	quietly summ sd_indcf5_lagatl1 if e(sample), det
		scalar cfvol_sd = r(sd)
	quietly summ depvar if e(sample), det
		scalar depvar_me = r(mean)	
	quietly summ rho if e(sample), det
		scalar rho_sd = r(sd)	
	quietly summ sigma_P if e(sample), det
		scalar sigP_sd = r(sd)			
	quietly summ sigma_A if e(sample), det
		scalar sigA_sd = r(sd)	
	* Evaluate effects
	nlcom	(RMEffCashVol     : _b[sd_indcf5_lagatl1]*cfvol_sd/depvar_me) ///
			(RMEffrho         : _b[rho]*rho_sd/depvar_me) ///		
			(RMEffsigP        : _b[sigma_P]*sigP_sd/depvar_me) ///
			(RMEffsigA        : _b[sigma_A]*sigA_sd/depvar_me) , post
est store econ_ife2

**** Net equity issuance to assets
drop depvar
g depvar = net_equity_issue2_at
*
reghdfe depvar rho sigma_P sigma_A $c if dummy1 == 1, a(fyear) vce(cluster sic3)

* This estimation bootstraps the standard errors
*areg depvar rho sigma_P sigma_A $c if dummy1 == 1, a(fyear) vce(boot, seed(10101) reps(100) cluster(sic3) id(uisic3) group(gvkey))
est store ols3
	* Econ significance: evaluation points
	quietly summ sd_indcf5_lagatl1 if e(sample), det
		scalar cfvol_sd = r(sd)
	quietly summ depvar if e(sample), det
		scalar depvar_me = r(mean)	
	quietly summ rho if e(sample), det
		scalar rho_sd = r(sd)	
	quietly summ sigma_P if e(sample), det
		scalar sigP_sd = r(sd)			
	quietly summ sigma_A if e(sample), det
		scalar sigA_sd = r(sd)	
	* Evaluate effects
	nlcom	(RMEffCashVol     : _b[sd_indcf5_lagatl1]*cfvol_sd/depvar_me) ///
			(RMEffrho         : _b[rho]*rho_sd/depvar_me) ///		
			(RMEffsigP        : _b[sigma_P]*sigP_sd/depvar_me) ///
			(RMEffsigA        : _b[sigma_A]*sigA_sd/depvar_me) , post
est store econ_ols3
*
reghdfe depvar rho sigma_P sigma_A $c if dummy1 == 1, a(fyear sic2) vce(cluster sic3)

* This estimation bootstraps the standard errors
*areg depvar rho sigma_P sigma_A $c i.fyear if dummy1 == 1, a(sic2) vce(boot, seed(10101) reps(100) cluster(sic3) id(uisic3) group(gvkey))
est store ife3
	* Econ significance: evaluation points
	quietly summ sd_indcf5_lagatl1 if e(sample), det
		scalar cfvol_sd = r(sd)
	quietly summ depvar if e(sample), det
		scalar depvar_me = r(mean)	
	quietly summ rho if e(sample), det
		scalar rho_sd = r(sd)	
	quietly summ sigma_P if e(sample), det
		scalar sigP_sd = r(sd)			
	quietly summ sigma_A if e(sample), det
		scalar sigA_sd = r(sd)	
	* Evaluate effects
	nlcom	(RMEffCashVol     : _b[sd_indcf5_lagatl1]*cfvol_sd/depvar_me) ///
			(RMEffrho         : _b[rho]*rho_sd/depvar_me) ///		
			(RMEffsigP        : _b[sigma_P]*sigP_sd/depvar_me) ///
			(RMEffsigA        : _b[sigma_A]*sigA_sd/depvar_me) , post
est store econ_ife3

**** Net equity issuance dummy
drop depvar
g depvar = eq_issue_dummy
*
reghdfe depvar rho sigma_P sigma_A $c if dummy1 == 1, a(fyear) vce(cluster sic3)

* This estimation bootstraps the standard errors
*areg depvar rho sigma_P sigma_A $c if dummy1 == 1, a(fyear) vce(boot, seed(10101) reps(100) cluster(sic3) id(uisic3) group(gvkey))
est store ols4
	* Econ significance: evaluation points
	quietly summ sd_indcf5_lagatl1 if e(sample), det
		scalar cfvol_sd = r(sd)
	quietly summ depvar if e(sample), det
		scalar depvar_me = r(mean)	
	quietly summ rho if e(sample), det
		scalar rho_sd = r(sd)	
	quietly summ sigma_P if e(sample), det
		scalar sigP_sd = r(sd)			
	quietly summ sigma_A if e(sample), det
		scalar sigA_sd = r(sd)	
	* Evaluate effects
	nlcom	(RMEffCashVol     : _b[sd_indcf5_lagatl1]*cfvol_sd/depvar_me) ///
			(RMEffrho         : _b[rho]*rho_sd/depvar_me) ///		
			(RMEffsigP        : _b[sigma_P]*sigP_sd/depvar_me) ///
			(RMEffsigA        : _b[sigma_A]*sigA_sd/depvar_me) , post
est store econ_ols4
*
reghdfe depvar rho sigma_P sigma_A $c if dummy1 == 1, a(fyear sic2) vce(cluster sic3)

* This estimation bootstraps the standard errors
*areg depvar rho sigma_P sigma_A i.fyear $c if dummy1 == 1, a(sic2) vce(boot, seed(10101) reps(100) cluster(sic3) id(uisic3) group(gvkey))
est store ife4
	* Econ significance: evaluation points
	quietly summ sd_indcf5_lagatl1 if e(sample), det
		scalar cfvol_sd = r(sd)
	quietly summ depvar if e(sample), det
		scalar depvar_me = r(mean)	
	quietly summ rho if e(sample), det
		scalar rho_sd = r(sd)	
	quietly summ sigma_P if e(sample), det
		scalar sigP_sd = r(sd)			
	quietly summ sigma_A if e(sample), det
		scalar sigA_sd = r(sd)	
	* Evaluate effects
	nlcom	(RMEffCashVol     : _b[sd_indcf5_lagatl1]*cfvol_sd/depvar_me) ///
			(RMEffrho         : _b[rho]*rho_sd/depvar_me) ///		
			(RMEffsigP        : _b[sigma_P]*sigP_sd/depvar_me) ///
			(RMEffsigA        : _b[sigma_A]*sigA_sd/depvar_me) , post
est store econ_ife4




label var rho "Rho"
label var sigma_P "Sigma_P: Permanent volatility"
label var sigma_A "Sigma_A: Transitory volatility"
label var sd_indcf5_atl1 "Lagged cash flow-to-assets volatility"
label var l_al1 "Lagged log(total assets)"
label var cf5_atl1 "Lagged cash flow-to-assets ratio"
label var cf5_at "Cash flow-to-lagged assets ratio"
label var mbl1 "Lagged MB ratio"
label var mbresid "Residuals of lagged MB ratio"

cd "$data"

estout ols1 ife1 ols2 ife2 ols3 ife3 ols4 ife4 using "Table9.txt", replace 	///
		   mlabels("G.eq.OLS" "G.eq.IFE" "Geq.dum.OLS" "Geq.dum.IFE" "N.eq.OLS" "N.eq.IFE" "NEq.dum.OLS" "NEq.dum.IFE") num  ///
		   cells(b(star fmt(%9.3f)) se(par)) stats(N r2 r2_a, fmt(%9.3g %9.0g) labels(Observations R2 "Adjusted R2")) ///
		   legend  label varlabels(_cons Constant) abbrev starl(* 0.1 ** 0.05 *** 0.01) ///
		   order(rho sigma_P sigma_A) ///
		   title("Table: Dependent variables are gross equity issuance to assets, gross eq. issuance dummy, net equity issuance to assets, and net equity issue dummy. Clustering of SEs at the SIC3 level.") ///	
		   varwidth(45) modelwidth(10) delimiter("") style(fixed) drop() wrap 
		   
estout  econ_ols1 econ_ife1 econ_ols2 econ_ife2 econ_ols3 econ_ife3 econ_ols4 econ_ife4 using "Table9.txt", append 		///
		   cells( b(star fmt(%9.3f)) se(par)) ///
		   legend label abbrev starl(* 0.1 ** 0.05 *** 0.01) ///
		   order(RMEffrho RMEffsigP RMEffsigA RMEffCashVol) ///
		   title("Table: Panel B. Economic effects: MEffVar is Delta E(y) = dE(y)/d(Var) * Std(Var), RMEffVar is MEffVar / E(y)") ///	
		   varwidth(45) modelwidth(10) delimiter("") style(fixed) wrap	

		   
*
